package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService {

    @Autowired
    BrandCategoryMapper brandCategoryMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void bind(Long brandId, Long categoryId) {
        log.debug("开始处理【插入品牌与类别的关联】的业务，品牌ID：{}，类别ID：{}", brandId, categoryId);
        BrandStandardVO brand = brandMapper.getStandardById(brandId);
        //品牌不存在
        if(brand == null){
            String message = "绑定品牌与类别的关联失败，尝试添加的品牌数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //品牌被禁用
        if(brand.getEnable() == 0){
            String message = "绑定品牌与类别的关联失败，尝试添加的品牌数据已经被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //类别不存在
        CategoryStandardVO category = categoryMapper.getStandardById(categoryId);
        if(category == null){
            String message = "绑定品牌与类别的关联失败，尝试添加的类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //类别被禁用
        if(category.getEnable() == 0){
            String message = "绑定品牌与类别的关联失败，尝试添加的类别数据已经被禁用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //重复绑定
        List<BrandCategoryListItemVO> list = brandCategoryMapper.list();
        for (BrandCategoryListItemVO brandCategoryListItemVO : list) {
            if(brandCategoryListItemVO.getBrandId().equals(brandId)
                    && brandCategoryListItemVO.getCategoryId().equals(categoryId)){
                String message = "绑定品牌与类别的关联失败，不允许重复绑定！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }
        BrandCategory brandCategory = new BrandCategory();
        brandCategory.setBrandId(brandId);
        brandCategory.setCategoryId(categoryId);
        brandCategoryMapper.insert(brandCategory);
        log.debug("绑定完成！品牌ID：{}，类别ID：{}", brandId, categoryId);
    }

    @Override
    public void unbind(Long id) {
        log.debug("开始处理【解绑品牌与类别的关联】业务，ID：{}", id);
        BrandCategoryStandardVO queryResult = brandCategoryMapper.getStandardById(id);
        if(queryResult == null){
            String message = "解绑品牌与类别的关联失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        brandCategoryMapper.deleteById(id);
        log.debug("解绑品牌与类别的关联成功！");
    }
}
